const baseURL = "@/utils/util.js"


export default function fetch(options) {
  return new Promise((resolve, reject) => {
    let {
      url,
      data: reqParams,
      method = 'POST',
      isShowLoading = false
    } = options

    let token = uni.getStorageSync('token')
    let header = token ? {
      'Authorization': token
    } : {}
    try {
      if (method.toUpperCase() == 'POST' || method.toUpperCase() == 'PUT') {
        header['content-type'] = 'application/x-www-form-urlencoded'
      }
      // 显示加载
      if (isShowLoading) {
        uni.showLoading({
          title: '加载中'
        })
      }

      uni.request({
        url: `${baseURL + url}`,
        header,
        method,
        data: reqParams,
        success(responseData) {
          if (isShowLoading) {
            uni.hideLoading()
          }
          let {
            data: response
          } = responseData
          // 200：接口成功
          // 201：接口失败
          // 500：服务器繁忙
          // 405 当前用户未登录
          // 403 未能找到数据
          // 408 参数有误

          // 报错
          if (Object.prototype.toString.call(response) == "[object String]") {
            return reject()
          }

          // 单独处理获取openid接口，改返回格式不一样
          if (url.indexOf('Api/getOpenId') != -1) {
            return resolve(response)
          }


          if (response.code != 200 && url.indexOf('Api/addUse') == -1 && url.indexOf('Article/zan') == -1) {

            uni.showToast({
              title: response.msg || '',
              icon: "none"
            })
            reject(response)
            return false
          }

          resolve(response.data)
        },
        fail(err) {
          console.error(err)
          reject(err)
        }
      });
    } catch (error) {
      console.error("request err", error)
      if (isShowLoading) {
        uni.hideLoading()
      }
      if (error && (error.errMsg || error == 'TypeError: Failed to fetch')) {
        uni.showToast({
          title: '网络错误',
          icon: "none"
        })
      }
      reject(error)
    }
  })
}